"""
Case Type   : HugePage
Case Name   : enable_huge_pages参数正常设置验证
Create At   : 2024/10/21
Owner       : liu-tong-8848
Description :
              1、连接数据库，查询enable_huge_pages参数，默认为off
              2、设置参数为1，重启数据库，验证参数为on
              3、设置参数为0，重启数据库，验证参数为off
              4、设置参数为true，重启数据库，验证参数为on
              5、设置参数为false，重启数据库，验证参数为off
              6、设置参数为on，重启数据库，验证参数为on
              7、设置参数为off，重启数据库，验证参数为off
Expect      :
              1、成功
              2、成功
              3、成功
              4、成功
              5、成功
              6、成功
              7、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_root_node = Node(node='PrimaryRoot')

    def test_startdb(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        self.log.info("设置大页内存，保证数据库启动")
        cat_hugepagesize_cmd = "grep -i Hugepagesize /proc/meminfo"
        change_hugepages_result = self.db_root_node.sh(cat_hugepagesize_cmd).result()
        self.assertIn('2048 kB', change_hugepages_result, "大页内存不为2048kB")
        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "申请大页内存属主失败")
        select_groupid_cmd = "id -g"
        groupid = self.db_user_node.sh(select_groupid_cmd).result()
        self.log.info("groupid: " + groupid)
        self.assertNotIn('error', groupid, "获取groupid失败")
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=" + groupid
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "修改大页内存属主失败")
        use_sysctl_cmd = "sysctl -p"
        use_sysctl_result = self.db_root_node.sh(use_sysctl_cmd).result()
        self.assertTrue(use_sysctl_result, "刷新系统参数失败")

        step1 = '--step1:连接数据库，查询enable_huge_pages参数，默认为off;expect:成功'
        self.log.info(step1)
        self.log.info("查询该参数默认值")
        check = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('off', check, "执行失败" + step1)

        step2 = '--step2:设置参数为1，重启数据库，验证参数为on;expect:成功'
        self.log.info(step2)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=1"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step2)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('on', result, "执行失败" + step2)

        step3 = '--step3:设置参数为1，重启数据库，验证参数为off;expect:成功'
        self.log.info(step3)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=0"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step3)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('off', result, "执行失败" + step3)

        step4 = '--step4:设置参数为true，重启数据库，验证参数为on;expect:成功'
        self.log.info(step4)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=true"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step4)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('on', result, "执行失败" + step4)

        step5 = '--step5:设置参数为false，重启数据库，验证参数为off;expect:成功'
        self.log.info(step5)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=false"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step5)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('off', result, "执行失败" + step5)

        step6 = '--step6:设置参数为on，重启数据库，验证参数为on;expect:成功'
        self.log.info(step6)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=on"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step6)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('on', result, "执行失败" + step6)

        step7 = '--step7:设置参数为off，重启数据库，验证参数为off;expect:成功'
        self.log.info(step7)
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=off"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "执行失败" + step7)
        self.primary_sh.restart_db_cluster()
        result = self.primary_sh.execut_db_sql("show enable_huge_pages;")
        self.assertIn('off', result, "执行失败" + step7)


    def tearDown(self):
        self.log.info("guc方式恢复默认值")
        gs_guc_set_cmd = '''source ''' + macro.DB_ENV_PATH + ''';gs_guc set -D ''' + macro.DB_INSTANCE_PATH \
                         + ''' -c "enable_huge_pages=off"'''
        gs_guc_set_result = self.db_user_node.sh(gs_guc_set_cmd).result()
        self.assertNotIn('ERROR', gs_guc_set_result, "恢复默认值失败")
        self.assertIn('enable_huge_pages=off', gs_guc_set_result, "恢复默认值失败")
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
